package com.synygy.bdviewer.ui;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.*;
import org.apache.wicket.model.*;
import org.apache.wicket.protocol.http.WebRequest;
import org.wicketstuff.dojo.markup.html.dialog.DojoDialog;
import org.wicketstuff.dojo.markup.html.dialog.DojoDialogOpener;
import org.wicketstuff.dojo.markup.html.dialog.DojoDialogCloser;
import org.wicketstuff.dojo.toggle.DojoWipeToggle;


import com.synygy.bdviewer.model.STIObject;
import com.synygy.bdviewer.dao.STIObjectDataProvider;
import com.synygy.bdviewer.dao.STIObjectDao;

import java.util.Arrays;
import java.util.List;


public class Home extends BasePage {

    @SpringBean(name = "stiObjectDao")
    STIObjectDao stiObjectDao;

    private final DefaultDataTable<STIObject> stidatatable;

    public Home() {

        STIObjectDataProvider dataProvider = new STIObjectDataProvider(stiObjectDao);

        // create the data table
        IColumn[] columns = createColumns();
        stidatatable = new DefaultDataTable("stilist", Arrays.asList(columns), dataProvider, 4);
        //stidatatable.addTopToolbar(new FilterToolbar(users, form, dataProvider));


        add(new Label("ipaddress", ((WebRequest) getRequest()).getHttpServletRequest().getRemoteAddr()));
        add(stidatatable);

    }

    IColumn[] createColumns() {

        IColumn[] columns = new IColumn[8];
        columns[0] = createColumn("id", "id", "id");
        columns[1] = createColumn("version", "version", "version");

//        columns[1]=new AbstractColumn(new PropertyModel<String>(this,"version")){
//            public void populateItem(Item item, String s, IModel iModel) {
//                 item.add(new Link("bla"){
//
//                     public void onClick() {
//
//                     }
//                 }) ;
//            }
//        }
//                ;
        columns[2] = createColumn("moduleOrFeature", "moduleOrFeature", "moduleOrFeature");
        columns[3] = createColumn("rootCause", "rootCause", "rootCause");
        columns[4] = createColumn("defectLogged", "defectLogged", "defectLogged");
        columns[5] = createColumn("personResponsible", "personResponsible", "personResponsible");
        columns[6] = createColumn("success", "success", "success");
        columns[7] = createColumn("date", "date", "date");
        return columns;
    }

    private TextFilteredPropertyColumn<String> createColumn(String key, String sortProperty,
                                                            String propertyExpression) {
        return new TextFilteredPropertyColumn<String>(new ResourceModel(key), sortProperty,
                propertyExpression);
    }

/**
 * Create a composite column extending FilteredAbstractColumn. This column
 * adds a UserActionsPanel as its cell contents. It also provides the
 * go-and-clear filter control panel.
 */
//    private FilteredAbstractColumn<Object> createActionsColumn() {
//        return new FilteredAbstractColumn<Object>(new Model<String>(getString("actions"))) {
//            // return the go-and-clear filter for the filter toolbar
//            public Component getFilter(String componentId, FilterForm form) {
//                return new GoAndClearFilter(componentId, form, new ResourceModel("filter"),
//                        new ResourceModel("clear"));
//            }
//
//            // add the UserActionsPanel to the cell item
//            public void populateItem(Item cellItem, String componentId, IModel model) {
//                cellItem.add(new UserActionsPanel(componentId, model));
//            }
//        };
}

//        add(new Label("ipaddress", ((WebRequest) getRequest()).getHttpServletRequest().getRemoteAddr()));
//        System.out.println(stiObjectService.getAllSTIObjects());
//        PageableListView<STIObject> stilist= new PageableListView("stilist", stiObjectService.getAllSTIObjects(), 5) {
//            @Override
//            protected void populateItem(ListItem item) {
//                STIObject stiObject=(STIObject)item.getModelObject();
//
//                item.add(new Label("id",stiObject.getId().toString()));
//                item.add(new Label("date",stiObject.getDate().toString()));
//                item.add(new Label("version",stiObject.getVersion()));
//                item.add(new Label("success",""+stiObject.isSuccess()));
//                item.add(new Label("defectLogged",stiObject.getDefectLogged()));
//                item.add(new Label("moduleOrFeature",stiObject.getModuleOrFeature()));
//                item.add(new Label("rootCause",stiObject.getRootCause()));
//                item.add(new Label("personResponsible",stiObject.getPersonResponsible()));
//
//            }
//        };
//        add(stilist);
//
//        add(new PagingNavigator("navigator",stilist));
//    }



